package com.augn.service.impl;

import com.augn.entity.Address;
import com.augn.entity.GoodsEvaluateFrom;
import com.augn.entity.Result;
import com.augn.entity.User;
import com.augn.mapper.AddressMapper;
import com.augn.service.IAddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private AddressMapper mapper;
    @Override
    public Result<?> insertOneAddress(Address address) {
        int insert = mapper.insert(address);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    @Override
    public Result<?> deleteOneAddress(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    @Override
    public Result<?> updateOneAddress(Address address) {
        int update = mapper.updateById(address);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    @Override
    public Result<?> getOneAddress(Integer id) {
        Address address = mapper.selectById(id);
        if(address==null){
            return Result.error("-1","无此地址");
        } else{
            return Result.success(address);
        }
    }

    @Override
    public Result<?> getPartAddress(Integer id) {
        QueryWrapper<Address> wrapper= new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<Address> addressList = mapper.selectList(wrapper);
        if(addressList.isEmpty()){
            return Result.error("-1","暂无地址");
        } else{
            return Result.success(addressList);
        }
    }
}
